using System;
using System.Windows.Forms;
using BLL;
using Common;
using Model;

namespace UI {
    public partial class FrmCategoryUpdate : Form {
        private Category _category;
        private CategoryBLL _categoryBll = CategoryBLL.GetInstance();
        public FrmCategoryUpdate() {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }

        private void FrmCategoryUpdate_Load(object sender, EventArgs e) {
            if (_category != null) {
                txtCid.Text = _category.CatId.ToString();
                txtCname.Text = _category.CatName;
                txtDesc.Text = _category.Remark;
                registDate.Value = _category.SubTime;
            }
        }

        private void button1_Click(object sender, EventArgs e) {
            if (_category == null) {
                _category = new Category();
            }
            _category.CatName = txtCname.Text;
            _category.Remark = txtDesc.Text;
            _category.SubTime = registDate.Value;
            Result<Category> result =  _categoryBll.UpdateCategory(_category);
            
            MessageBox.Show(result.Message);
            if (result.IsSuccess) {
                DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        public void Init_category(Category category) {
            _category = category;
        }
    }
}